#include "UserMgr.hpp"
#include "CSession.hpp"
#include "RedisMgr.hpp"
#include <memory>
#include <mutex>
#include <string>

UserMgr::UserMgr() {}

UserMgr::~UserMgr() { _uidToSession.clear(); }

std::shared_ptr<CSession> UserMgr::GetSession(int uid) {
  std::lock_guard<std::mutex> lock(_mutex);
  auto iter = _uidToSession.find(uid);
  if (iter == _uidToSession.end()) {
    return nullptr;
  }
  return iter->second;
}

void UserMgr::SetUserSession(int uid, std::shared_ptr<CSession> session) {
  std::lock_guard<std::mutex> lock(_mutex);
  _uidToSession[uid] = session;
}

void UserMgr::RemoveUserSession(int uid) {
  auto uid_str = std::to_string(uid);
  {
    std::lock_guard<std::mutex> lock(_mutex);
    _uidToSession.erase(uid);
  }
}